/**
 * 拖拽
 * @constructor
 */
export function Drag(options, shapes, graphicTreeData) {

  // console.log(options)

  /**
   *
   * @param el 节点
   */
  this.register = function (el) {
    // console.log(el)

    el.addEventListener('mousedown', function (e) {

      if (e.button != 0) {
        //屏蔽左键以外的按键
        return;
      }

      //获取x坐标和y坐标
      var x = e.clientX;
      var y = e.clientY;

      //获取左部和顶部的偏移量
      var l = el.offsetLeft;
      var t = el.offsetTop;

      if (options && options.onBegin) {
        options.onBegin.call(el, {
          blockLeft: x - l,
          blockTop: y - t
        });
      }


      //开关打开
      var isDown = true;
      //设置样式
      el.style.cursor = 'move';

      var nl = x, nt = y;


      window.onmousemove = function (e) {
        if (!isDown) {
          return;
        }
        //获取x和y
        var nx = e.clientX;
        var ny = e.clientY;

        //计算移动后的左偏移量和顶部的偏移量
        nl = nx - (x - l);
        nt = ny - (y - t);

        //拖动区域限制 start
        var id = el.getAttribute('data-id')
        var parentId = shapes.filter(item => item.id === id)[0].parentId
        var myFather = getMyFather(graphicTreeData, parentId)
        var parentEl = document.getElementById(parentId)
        if (parentEl && myFather && myFather.blockLevel >1) {
          //限制左右拖拽
          if (nl < parentEl.offsetLeft +5) {
            nl = parentEl.offsetLeft + 5
          }
          if (nl + el.clientWidth > parentEl.clientWidth + parentEl.offsetLeft -5) {
            nl = parentEl.clientWidth + parentEl.offsetLeft - el.clientWidth - 5
          }
          //限制上下拖拽
          if (nt < parentEl.offsetTop +5) {
            nt = parentEl.offsetTop + 5
          }
          if (nt + el.clientHeight > parentEl.clientHeight + parentEl.offsetTop -5) {
            nt = parentEl.clientHeight + parentEl.offsetTop - el.clientHeight - 5
          }
        }
        //拖动区域限制 end
        el.style.left = nl + 'px';
        el.style.top = nt + 'px';

        if (options && options.onDrag) {
          options.onDrag.call(el, {
            blockLeft: nl,
            blockTop: nt,
            x: nx - x,
            y: ny - y
          });
        }

        return false;
      }

      window.onmouseup = function (e) {
        //开关关闭
        isDown = false;
        el.style.cursor = 'default';

        if (options && options.onEnd) {
          options.onEnd.call(el, {left: nl, top: nt});
        }

        return false;
      }
      // e.stopPropagation();
      if (e.stopPropagation) {
        e.stopPropagation();
      } else if (e.preventDefault) {
        e.preventDefault();
      } else {
        window.event.returnValue == false;
      }
    });

    function getMyFather(list, id) {
      for (let i = 0; i < list.length; i++) {
        let treeItem = list[i]
        if (treeItem.id === id) {
          //找到后return
          return treeItem
        } else {
          if (treeItem.children && treeItem.children.length > 0) {
            let res = getMyFather(treeItem.children, id)
            if (res) {
              return res
            }
          }
        }
      }
    }
  }
}
